package com.example.timertaskexample;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	Button btn;
	SeekBar seekbar;
	
	Timer timer;
	TimerTask task;
	Handler handler;
	int myprogress = 0;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btn = (Button) this.findViewById(R.id.button1);
		seekbar = (SeekBar) this.findViewById(R.id.seekBar1);
		
		timer = new Timer();
		task = new TimerTask(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				myprogress++;
				
				Message msg = new Message();
				msg.what = 1;
				handler.sendMessage(msg);
			}};
			
		handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if(msg.what == 1)
				{
				seekbar.setProgress(myprogress);
				}
				
				super.handleMessage(msg);
			}};
		
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				timer.schedule(task, 1000, 1000);
			}
		});
		
		
		seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				myprogress = seekBar.getProgress();
				Toast.makeText(MainActivity.this, myprogress+"%", Toast.LENGTH_SHORT).show();
				return ;
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				return ;			
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				
			}
		});
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
